package com.marver.sys.service;

import com.marver.common.dao.BaseDAOImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 部门 service
 * @Author: Marver
 * @date: 2018-05-02 10:14
 **/
@Service
public class DeptService extends BaseDAOImpl {

    /**
     * 根据父级部门ID 查询下级部门ID
     * @param parentId
     * @return
     */
    public List<Long> querDeptIdList(Long parentId){
        return (List<Long>) this.listBySqlMap("dept.queryDetpIdList", parentId);
    }

    public List<Long> getSubDeptIdList(Long deptId){
        //部门及子部门ID列表
        List<Long> deptIdList = new ArrayList<>();
        //获取子部门ID
        List<Long> subDeptIdList = this.querDeptIdList(deptId);
        getDeptTreeList(subDeptIdList, deptIdList);
        return deptIdList;
    }

    /**
     * 递归获取闭门id
     * @param subIdList
     * @param deptIdList
     */
    private void getDeptTreeList(List<Long> subIdList, List<Long> deptIdList){
        subIdList.forEach(d->{
            List<Long> list = querDeptIdList(d);
            if(list.size()>0){
                getDeptTreeList(list, deptIdList);
            }
            deptIdList.add(d);
        });
    }


}
